package com.spider.config;

import org.quartz.Scheduler;
import org.quartz.ee.servlet.QuartzInitializerListener;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import java.io.IOException;
import java.util.Properties;

@Configuration
public class ScheduleConfig {


    @Bean(name="schedulerFactory")
    public SchedulerFactoryBean schedulerFactoryBean() throws IOException{
        SchedulerFactoryBean factoryBean = new SchedulerFactoryBean();
        factoryBean.setQuartzProperties(getQuartzProperties());
        return factoryBean;
    }

    @Bean
    public Properties getQuartzProperties() throws IOException{
        PropertiesFactoryBean propertiesFactoryBean =
                new PropertiesFactoryBean();
        //加载本地属性文件
        propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
        //初始化文件
        propertiesFactoryBean.afterPropertiesSet();
        return propertiesFactoryBean.getObject();
    }

    @Bean
    public QuartzInitializerListener listener(){
        return new QuartzInitializerListener();
    }

    @Bean
    public Scheduler scheduler() throws IOException{
        return schedulerFactoryBean().getScheduler();
    }

}
